package Greedy._435_Non_overlappingIntervals;

import com.sun.corba.se.impl.naming.cosnaming.InternalBindingValue;

import java.util.Arrays;
import java.util.Comparator;

public class _435_Non_overlappingIntervals {
    public int eraseOverlapIntervals(Interval[] intervals) {
        if (intervals.length == 0) {
            return 0;
        }
        Arrays.sort(intervals, new endComparator());
        int end = intervals[0].end;
        int count = 1;
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i].start >= end) {
                count++;
                end = intervals[i].end;
            }
        }
        return intervals.length - count;
    }

    class endComparator implements Comparator<Interval> {
        @Override
        public int compare(Interval o1, Interval o2) {
            return o1.end-o2.end;
        }
    }

}
